from machine import Pin, SPI, PWM
import utime as time

# red = Pin(3,Pin.OUT)
# green = Pin(28,Pin.OUT)
# blue = Pin(22,Pin.OUT)

# T = 1ms = 1000us = 10^6ns
red = PWM(Pin(3), freq=1000, duty_ns=0)
green = PWM(Pin(28), freq=1000, duty_ns=0)
blue = PWM(Pin(22), freq=1000, duty_ns=0)

# 非线性
# def led256(r, g, b):
#     red.duty_ns(int(3 ** (0.05 * r)) - 1)
#     green.duty_ns(int(3 ** (0.05 * g)) - 1)
#     blue.duty_ns(int(3 ** (0.05 * b)) - 1)

# 线性
def led256(r, g, b):
    red.duty_ns(int(3906.35*r))
    green.duty_ns(int(3906.35*g))
    blue.duty_ns(int(3906.35*b))



if __name__ == '__main__':
    # led = [red, green, blue]
    # for e in led:
    #     for i in range(125):
    #         e.duty_ns(int(3 ** (i / 10)))
    #         time.sleep(0.01)
    #     e.duty_ns(0)

    led256(122, 122, 134)
    time.sleep(3)
    led256(153, 61, 153)
    time.sleep(4)
    led256(0,0,0)



